<?php

require_once('../application/modules/game/models/DbTable/Games.php');
require_once('../application/modules/game/forms/AddGame.php');

class Game_IndexController extends Zend_Controller_Action {

    public function init() {
   
    }

    public function indexAction() {
        $dbGame = new Game_Model_DbTable_Games();
        $this->view->title = "GameList";
        $data = $dbGame->fetchAll()->toArray();      
        $currency = new Zend_Currency();
        $currency->setLocale($this->get_locale());
        foreach($data as $key => $value){
            $data[$key]["price"] = $currency->toCurrency($value['price']);
        }
        
        $this->view->aData = $data;
    }

    function get_locale(){
        switch($_SESSION['lang']){
            case "de":
                return "de_DE"; break;
            case "fr":
                return "fr_FR"; break;
            case "en":
                return "en_GB"; break;
            case "es":
                return "es_ES"; break;
        }
    }
    public function addAction() {
        $dbGames = new Game_Model_DbTable_Games;
        $form = new Game_Form_AddGame();
        $form->getElement('name')->setLabel($this->view->t("Name"));
        $form->getElement('price')->setLabel($this->view->t("Price"));
        $form->getElement('description')->setLabel($this->view->t("Description"));
        $this->view->title = "Ajouter un jeu";
        if ($this->getRequest()->isPost()) {
            $data = $this->_request->getParams();
            if ($form->isValid($data)) {
                $dbGames->createRow();
                $lastInsertId = $dbGames->addGame($data);
                if ($lastInsertId) {
                    $this->_helper->redirector('detail', 'index', 'game', array('id' => $lastInsertId));
                }
            } else {
                $form->populate($data);
            }
        }
        $this->view->form = $form;
    }

    public function deleteAction() {
        $this->view->title = "Supprimer un jeu";
        $dbGames = new Game_Model_DbTable_Games();
        $id = $this->_request->getParam('id');
        $row = $dbGames->find($id)->toArray();
        $this->view->data = current($row);
        if ($this->getRequest()->isPost()) {
            if ($this->_request->getPost('submit')) {
                $dbGames->deleteGame($id);
            }
            $this->_helper->redirector('index', 'index', 'game');
        }
    }

    public function editAction() {
        $this->view->title = "Editer un jeu";
        $dbGames = new Game_Model_DbTable_Games();
        $form = new Game_Form_AddGame;
        $form->getElement('name')->setLabel($this->view->t("Name"));
        $form->getElement('price')->setLabel($this->view->t("Price"));
        $form->getElement('description')->setLabel($this->view->t("Description"));
        $id = $this->_request->getParam('id');

        if ($this->getRequest()->isPost()) {
            $data = $this->_request->getPost();
            if ($form->isValid($data)) {
                $updated = $dbGames->updateGame($id, $data);
            } else {
                $form->populate($data);
            }
        }
        $row = $dbGames->find($id)->toArray();
        $form->populate(current($row));
        $this->view->aData = current($row);
        $this->view->form = $form;
    }

    public function detailAction() {
        $this->view->title = "Detail d'un jeu";
        $dbGames = new Game_Model_DbTable_Games();
        $id = $this->_request->getParam('id');
        $row = $dbGames->find($id)->toArray();
        
        $currency = new Zend_Currency();
        $currency->setLocale($this->get_locale());
        $row = current($row);
        $row['price'] = $currency->toCurrency($row['price']);
        $this->view->data = $row;
    }

    public function configAction() {
        $db = Zend_Db_Table::getDefaultAdapter();
        $post = $this->_request->getPost('config');
        $_SESSION['config'] = $post;
        switch ($post) {
            case "xml":
                $columnMapping = array('date' => 'message', 'level' => 'priority');
                $writer = new Zend_Log_Writer_Db($db, 'zf_specifical_log', $columnMapping);
                $logger = new Zend_Log($writer);
                $logger->info(date("Y-m-d H-i-s"));
                break;
            case "yaml":
                $writer = new Zend_Log_Writer_Firebug();
                $logger = new Zend_Log($writer);
                $logger->log("Config Yaml : " . date("Y-m-d H-i-s"), Zend_Log::INFO);
                break;
            case "json":
                $mail = new Zend_Mail();
                $mail->setFrom('zend_project.com')
                    ->addTo('dev@project.org');
                $writer = new Zend_Log_Writer_Mail($mail);
                $writer->setSubjectPrependText('Log mail');

                $log = new Zend_Log();
                $log->addWriter($writer);
                $log->error('Message de log');
            default:
                break;
        }
        $this->view->config = $post;

        //$this->_helper->redirector("index", "index", "game");
    }

    public function translateAction(){
        $post = $this->_request->getPost('lang');
        $_SESSION['lang'] = $post;
        
        $url = parse_url($_SERVER['HTTP_REFERER']);
        $url = explode('/',trim($url['path']));
        $url = array_splice($url, 1);
        $module = $url[0];
        $controller = isset($url[1]) ? $url[1] : 'index';
        $action = isset($url[2]) ? $url[2] : 'index';
        $params = isset($url[3]) && isset($url[4]) ? array($url[3] => $url[4]) : array();
        
        $this->_helper->redirector($action, $controller, $module,$params);
    }
}

